package de.idgard.android.addon.ui.component;

import de.idgard.android.R;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

/**
 * @author pratik popat
 *
 * 
 */
public class CustomWebProgressBar{

	private boolean isEnableProgressText;
	private Activity context;
	private TextView tvProgress;
	private View mProgressBar,mProgressBackground;
	
	private int total_width;
	
	public CustomWebProgressBar(Activity context, boolean enableProgressText) {
		this.context = context;
		isEnableProgressText = enableProgressText;
		initProgressBar();
		
	}
	
	public CustomWebProgressBar(Activity context, boolean enableProgressText,Drawable progress, Drawable progressBackground){}
	
	private void initProgressBar(){
		
		tvProgress = (TextView) context.findViewById(R.id.tv_progress);
		if(isEnableProgressText)
		tvProgress.setVisibility(View.VISIBLE);
		else
		tvProgress.setVisibility(View.GONE);
		
		mProgressBar = (View) context.findViewById(R.id.pb_view_progress);
		mProgressBackground = (View) context.findViewById(R.id.pb_view_background);
		
		setProgressColor(context.getApplicationContext().getResources().getDrawable(R.drawable.idgard_progressbar_yellow));
		setProgressBarBackGround(context.getApplicationContext().getResources().getDrawable(R.drawable.idgard_progressbar_green));

	}
	
	private void setProgressColor(final Drawable color) {
        mProgressBar.setBackgroundDrawable(color);
	}
	
	private void setProgressBarBackGround(final Drawable background) {
		mProgressBackground.setBackgroundDrawable(background);
	}
	
	public void setProgress(int progress){
		updateProgressBar(progress);
	}

	private void updateProgressBar(int progress) {
		
		tvProgress.setText("" + progress + " %");
		
		total_width = mProgressBackground.getWidth();
		
		LayoutParams params = mProgressBar.getLayoutParams();
		params.width = (progress * total_width / 100);
		mProgressBar.setLayoutParams(params);
	
		if (progress == 100) {
			params = mProgressBar.getLayoutParams();
		    params.width = 0;
		    mProgressBar.setLayoutParams(params);
	    }

	}
	
}
